import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: Home-pc
 * Date: 2023-10-01
 * Time: 15:14
 */
//利用Callable建立一个线程
public class Demo26 {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        Callable<Integer> callable =new Callable<Integer>() {
            @Override
            public Integer call() throws Exception {
                int sum=0;
                for (int i = 0; i <1000; i++) {
                    sum+=i;
                }
                return sum;
            }
        };
        //callable不能直接用，需要借用FutureTask这个标签
        FutureTask<Integer> futureTask=new FutureTask<>(callable);
        Thread t =new Thread(futureTask);
        t.start();

        System.out.println(futureTask.get());
    }
}
